package com.example.simpdptest;

import java.util.Iterator;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.aaa.R;

/**
 * 此控件是中间带滚动条的布局，不包含上下两个按钮
 * 
 * @Author ZGY
 * @Date:2014-3-27
 * @version
 * @since 欢迎加群88130145进行开发交流
 * 
 */
public class FloorScrollBarLand extends View {

    // 要画的图片
    private Drawable indoor_blue;
    private Drawable indoor_gray;

    private boolean mIsIndoorEnable=true;// 室内地图是否可用
    // 背景
    private int mBackGroudWidth;
    private int mBackGroudHeight;
    // 滑块
    private int mListviewHeight;
    private int mSeekHeight;
    private int mUpButtonHeight;
    private int marginToItem;
    private int mBarTop; // 滑块离顶部距
    private int mBarTop_last;//上次滑块离顶部距
    private OnBarLandListener mListener;

    private int mNowProgress;// 当前滚动的位置


    /**
     * 回调接口
     * 
     * @version ScrollBarView
     * @since
     */
    public interface OnBarLandListener {

	void onBarClick();

    }

    public FloorScrollBarLand(Context context, AttributeSet attrs) {
	super(context, attrs);
	init(context);
    }

    public FloorScrollBarLand(Context context) {
	super(context);
	init(context);
    }

    private void init(Context context) {
	indoor_blue = context.getResources().getDrawable(
		R.drawable.indoor_myfloor_icon_blue_land);// 定位可用图片
	indoor_gray = context.getResources().getDrawable(
		R.drawable.indoor_myfloor_icon_gray_land);// 定位不可用图片
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
	super.onSizeChanged(w, h, oldw, oldh);
	mBackGroudWidth = h;
	mBackGroudHeight = w;
	setWidthHeight();
    }

    /**
     * 控制滚动条、滚动条背景的宽高
     * 
     */
    private void setWidthHeight() {
	mListviewHeight = mBackGroudHeight - dip2px(getContext(), 44);// listview高度
	mUpButtonHeight=dip2px(getContext(), 22f);//上面按钮的宽度				   
	mSeekHeight = dip2px(getContext(), 21);// 滑块的高度
	
	marginToItem=(mListviewHeight/4-mSeekHeight)/2;//在listview上显示的时候，滑块距离item分割线的距离
	
//	setProgress(1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
	Paint paint = new Paint();
//	paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
//	canvas.drawPaint(paint);
//	paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
	Bitmap buffer = Bitmap.createBitmap(mBackGroudWidth, mBackGroudHeight, Bitmap.Config.ARGB_4444);
	buffer.eraseColor(Color.TRANSPARENT);	
	canvas.drawBitmap(buffer, 0, 0, paint);
	
	if (mIsIndoorEnable) {
	    indoor_blue.draw(canvas);
	} else {
	    indoor_gray.draw(canvas);
	}
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
	
	if(event.getY()>mBarTop && event.getY()<mBarTop+mSeekHeight){
	    if(mListener!=null){
		mListener.onBarClick();
	    }
	    return true;
	}
	
        return super.onTouchEvent(event);
        
        
    }

    /**
     * 获得当前滚动的位置
     * 
     * @param @return 
     * @author zhuanggy
     * @date 2014-3-28
     */
    public int getNowProgress() {
	return this.mNowProgress;
    }


    /**
     * 设置滚动条滚动位置
     * 
     * @param @param progress 当前楼层
     * progress=-1 代表最定的
     * progress=-2 代表最低端
     */
    public void setProgress(int progress) {
	// Log.e("", "progress=" +progress);
	mNowProgress = progress;
	if (mNowProgress == -1) {
	    mBarTop = 0; 
	    if (mIsIndoorEnable) {
		    indoor_blue.setBounds(mBarTop, 0, mBarTop + mSeekHeight, mSeekHeight);
		} else {
		    indoor_gray.setBounds(mBarTop, 0, mBarTop + mSeekHeight, mSeekHeight);
		}
		invalidate();
		mBarTop_last=mBarTop;
	}
	else if(mNowProgress==-2){
	    mBarTop = mBackGroudHeight-mSeekHeight;
	    if (mIsIndoorEnable) {
		    indoor_blue.setBounds(mBarTop, 0, mBarTop + mSeekHeight, mSeekHeight);
		} else {
		    indoor_gray.setBounds(mBarTop, 0, mBarTop + mSeekHeight, mSeekHeight);
		}
		invalidate();
		mBarTop_last=mBarTop;
	}
	else{
	    mBarTop=mUpButtonHeight+(progress*mListviewHeight/4)+marginToItem;
	    handler.sendEmptyMessage(0);
	
	}
    }
    
    
    private Handler handler=new Handler(){

	@Override
	public void handleMessage(Message msg) {
	    if (msg.what == 0) {
		if (mIsIndoorEnable) {
		    indoor_blue.setBounds(mBarTop, 0, mBarTop + mSeekHeight, mSeekHeight);
		} else {
		    indoor_gray.setBounds(mBarTop, 0, mBarTop + mSeekHeight, mSeekHeight);
		}
		invalidate();
	    }
	    
	    super.handleMessage(msg);
	    
	}
	
    };
    

    public boolean ismIsIndoorEnable() {
        return mIsIndoorEnable;
    }

    public void setmIsIndoorEnable(boolean mIsIndoorEnable) {
        this.mIsIndoorEnable = mIsIndoorEnable;
        invalidate();
    }

    /**
     * 设置滚动条监听
     * 
     * @param @param a_Listen 
     * @date 2014-3-26
     */
    public void setListener(OnBarLandListener a_Listen) {
	mListener = a_Listen;
    }

    public static int dip2px(Context context, float dipValue) {
	final float scale = context.getResources().getDisplayMetrics().density;
	return (int) (dipValue * scale + 0.5f);
    }
}
